package com.liuheng.spzx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liuheng.spzx.common.exception.CustomException;
import com.liuheng.spzx.model.dto.h5.UserLoginDto;
import com.liuheng.spzx.model.dto.h5.UserRegisterDto;
import com.liuheng.spzx.model.entity.user.UserInfo;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.model.vo.h5.UserInfoVo;
import com.liuheng.spzx.user.service.UserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("api/user/userInfo/")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        UserInfo userInfo = new UserInfo();
        String code = (String) redisTemplate.opsForValue().get(userRegisterDto.getUsername());
        if (code==null||code.equals(userRegisterDto.getCode())){
            throw new CustomException(ResultCodeEnum.SUCCESS);
        }
        BeanUtils.copyProperties(userRegisterDto,userInfo);
        userInfo.setPassword(DigestUtils.md5DigestAsHex(userRegisterDto.getPassword().getBytes()));
        userInfo.setStatus(1);
        userInfo.setSex(0);
        userInfo.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        userInfoService.save(userInfo);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userLoginDto!=null,UserInfo::getUsername,userLoginDto.getUsername());
        UserInfo userInfo = userInfoService.getOne(wrapper);
        if (userInfo.getStatus()==0){
            throw new CustomException(ResultCodeEnum.ACCOUNT_STOP);
        }
        if(userInfo!=null){
            String string = DigestUtils.md5DigestAsHex(userLoginDto.getPassword().getBytes());
            if(string.equals(userInfo.getPassword())){
                String token = UUID.randomUUID().toString().replaceAll("-", "");
                redisTemplate.opsForValue().set("user:spzx:" + token, userInfo, 30, TimeUnit.DAYS);
                UserInfo o = (UserInfo) redisTemplate.opsForValue().get("user:spzx:" + token);
                return Result.build(token,ResultCodeEnum.SUCCESS);
            }
            throw new CustomException(ResultCodeEnum.LOGIN_ERROR);
        }
        throw new CustomException(ResultCodeEnum.LOGIN_ERROR);
    }
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get("user:spzx:" + token);
        if(userInfo==null){
            throw new CustomException(ResultCodeEnum.LOGIN_ERROR);
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }
}
